package org.misty.practices.hashcode;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Misty on 2020-06-08
 */
public class CopySetHash {
//    public static final int hashMast;
//    public static final int hashShift;

    public static int right_n_bits(int n) {
        int x = 1;
        for (int i = 1; i < n; i++) {
            x = (x << 1) + 1;
        }
        return x;
    }

    public static void main(String[] args) {
//        System.out.println("lock_mask " + Integer.toBinaryString(right_n_bits(2)));
//        System.out.println("lock_mask_in_place " + Integer.toBinaryString(right_n_bits(2) << 0));

        int age_bits = 4; // age
        int lock_bits = 2; // lock
        int biased_lock_bits = 1; // biased_lock
        int max_hash_bits = 32 - age_bits - lock_bits - biased_lock_bits; // 32-4-2-1=25
        int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits; // identity_hashcode:25
        int cms_bits = 0; // 64位包含1位unused，32位没有
        int epoch_bits = 2; // Biased状态下的epoch

        int lock_shift = 0;
        int biased_lock_shift = lock_bits; // << 2
        int age_shift = lock_bits + biased_lock_bits; // << 3
        int cms_shift = age_shift + age_bits; // << 7
        int hash_shift = cms_shift + cms_bits; // << 7 (32位)
        int epoch_shift = hash_shift;

        int lock_mask = right_n_bits(2);
        int lock_mask_in_place = lock_mask << 0;
        int biased_lock_mask = right_n_bits(lock_bits + biased_lock_bits);
        int biased_lock_mask_in_place = biased_lock_mask << lock_shift;
        int biased_lock_bit_in_place = 1 << biased_lock_shift;
        int age_mask = right_n_bits(age_bits);
        int age_mask_in_place = age_mask << age_shift;
        int epoch_mask = right_n_bits(epoch_bits);
        int epoch_mask_in_place = epoch_mask << epoch_shift;
        int cms_mask = right_n_bits(cms_bits);
        int cms_mask_in_place = cms_mask << cms_shift;
        int hash_mask = right_n_bits(hash_bits);
        int hash_mask_in_place = hash_mask << hash_shift;

//        System.out.println(Integer.toBinaryString(~hash_mask_in_place));

        AtomicReference<String> ref = new AtomicReference<>();
        String old = ref.compareAndExchange(null, "B");
        System.out.println(old);
        System.out.println(ref.get());
    }
}
